package org.jeecg.modules.index;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.company.entity.CompanyStore;
import org.jeecg.modules.company.service.ICompanyStoreService;
import org.jeecg.modules.index.entity.Ecology;
import org.jeecg.modules.index.entity.Travel;
import org.jeecg.modules.index.service.IEcologyService;
import org.jeecg.modules.index.service.ITravelService;
import org.jeecg.modules.market.service.ICompanyPurchaseService;
import org.jeecg.modules.market.service.IRetailPurchaseService;
import org.jeecg.modules.market.service.IWholesalePurchaseService;
import org.jeecg.modules.plantation.entity.Plantation;
import org.jeecg.modules.plantation.service.IAgriculturalPointService;
import org.jeecg.modules.plantation.service.IPlantationService;
import org.jeecg.modules.system.entity.SysAnnouncement;
import org.jeecg.modules.system.service.ISysAnnouncementSendService;
import org.jeecg.modules.system.service.ISysAnnouncementService;
import org.jeecg.modules.weather.entity.Weather;
import org.jeecg.modules.weather.service.IWeatherService;
import org.jeecg.modules.wxchar.entity.NeedRelease;
import org.jeecg.modules.wxchar.service.INeedReleaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Description: 首页接口
* @Date:   2019-09-09
* @Version: V1.0
*/
@Slf4j
@Api(tags="首页接口")
@RestController
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private IPlantationService plantationService;

    @Autowired
    private IAgriculturalPointService agriculturalPointService;

    @Autowired
    private ICompanyStoreService companyStoreService;

    @Autowired
    private ICompanyPurchaseService companyPurchaseService;

    @Autowired
    private IRetailPurchaseService retailPurchaseService;

    @Autowired
    private IWholesalePurchaseService wholesalePurchaseService;

    @Autowired
    private IEcologyService ecologyService;

    @Autowired
    private ITravelService travelService;

    @Autowired
    private INeedReleaseService needReleaseService;

    @Autowired
    private IWeatherService weatherService;

    @Autowired
    private ISysAnnouncementService sysAnnouncementService;

    /**
     * 查询公告列表
     * @param sysAnnouncement
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "查询公告列表")
    @ApiOperation(value="查询公告列表", notes="查询公告列表")
    @GetMapping(value = "/sysAnnouncementList")
    public Result<IPage<SysAnnouncement>> sysAnnouncementList(SysAnnouncement sysAnnouncement,
                                                        @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                        @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                        HttpServletRequest req) {
        Result<IPage<SysAnnouncement>> result = new Result<IPage<SysAnnouncement>>();
        sysAnnouncement.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
        QueryWrapper<SysAnnouncement> queryWrapper = new QueryWrapper<SysAnnouncement>(sysAnnouncement);
        Page<SysAnnouncement> page = new Page<SysAnnouncement>(pageNo,pageSize);
        IPage<SysAnnouncement> pageList = sysAnnouncementService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 统计种植信息
     * @return
     */
    //@AutoLog(value = "统计种植信息")
    @ApiOperation(value="统计种植信息", notes="统计种植信息")
    @GetMapping(value = "/sumPlantantion")
    public Result<Plantation> sumPlantantion(
            @RequestParam(name="ruralTown",required=false) String ruralTown,
            @RequestParam(name="product",required=false) String product             ) {
        Result<Plantation> result = new Result<Plantation>();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("ruralTown",ruralTown);
        map.put("product",product);
        Plantation plantation = plantationService.sumPlantantion(map);
        result.setSuccess(true);
        result.setResult(plantation);
        return result;
    }


    /**
     * 历年产量
     * @return
     */
    //@AutoLog(value = "历年产量")
    @ApiOperation(value="历年产量", notes="历年产量")
    @GetMapping(value = "/sumActualOutput")
    public Result<List<Map<String ,Object>>> sumActualOutput(
            @RequestParam(name="ruralTown",required=false) String ruralTown,
            @RequestParam(name="product",required=false) String product ) {
        Result<List<Map<String ,Object>>> result = new Result<List<Map<String ,Object>>>();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("ruralTown",ruralTown);
        map.put("product",product);
        List<Map<String ,Object>> list  = plantationService.sumActualOutput(map);
        result.setSuccess(true);
        result.setResult(list);
        return result;
    }

    /**
     *   统计农资点
     * @param ruralTown
     * @return
     */
    //@AutoLog(value = "统计农资点")
    @ApiOperation(value="统计农资点", notes="统计农资点")
    @GetMapping(value = "/countAgriculturalPoint")
    public Result<Object> countAgriculturalPoint(@RequestParam(name="ruralTown",required=false) String ruralTown) {
        Result<Object> result = new Result<Object>();
        try {
            Integer count = agriculturalPointService.countAgriculturalPoint(ruralTown);
            result.setResult(count);
            result.success("统计成功！");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 统计企业存储结果
     * @return
     */
    //@AutoLog(value = "统计企业存储结果")
    @ApiOperation(value="统计企业存储结果", notes="统计企业存储结果")
    @GetMapping(value = "/sumCompanyStore")
    public Result<CompanyStore> sumCompanyStore() {
        Result<CompanyStore> result = new Result<CompanyStore>();
        CompanyStore companyStore = companyStoreService.sumCompanyStore();
        result.setSuccess(true);
        result.setResult(companyStore);
        return result;
    }


    /**
     * 统计企业收购
     * @param year
     * @return
     */
    //@AutoLog(value = "统计企业收购")
    @ApiOperation(value="统计企业收购", notes="统计企业收购")
    @GetMapping(value = "/avgCompanyPrice")
    public Result<List<Map<String,Object>>> avgCompanyPrice(@RequestParam(name="year", defaultValue="2019") String year) {
        Result<List<Map<String,Object>>> result = new Result<List<Map<String,Object>>>();
        try {
            List<Map<String,Object>> pageList = companyPurchaseService.avgCompanyPrice(year);
            result.setSuccess(true);
            result.setResult(pageList);
        }catch (Exception e){
            result.error500("系统错误");
        }
        return result;
    }


    /**
     * 统计企业收购量
     * @return
     */
    //@AutoLog(value = "统计企业收购量")
    @ApiOperation(value="统计企业收购量", notes="统计企业收购量")
    @GetMapping(value = "/groupByCompanyNumber")
    public Result<List<Map<String,Object>>> groupByCompanyNumber() {
        Result<List<Map<String,Object>>> result = new Result<List<Map<String,Object>>>();
        try {
            List<Map<String,Object>> pageList = companyPurchaseService.groupByCompanyNumber();
            result.setSuccess(true);
            result.setResult(pageList);
        }catch (Exception e){
            result.error500("系统错误");
        }
        return result;
    }

    /**
     * 统计零售情况
     * @param year
     * @return
     */
    //@AutoLog(value = "统计零售情况")
    @ApiOperation(value="统计零售情况", notes="统计零售情况")
    @GetMapping(value = "/avgRetailPrice")
    public Result<List<Map<String,Object>>> avgRetailPrice(@RequestParam(name="year", defaultValue="2019") String year) {
        Result<List<Map<String,Object>>> result = new Result<List<Map<String,Object>>>();
        try {
            List<Map<String,Object>> pageList = retailPurchaseService.avgRetailPrice(year);
            result.setSuccess(true);
            result.setResult(pageList);
        }catch (Exception e){
            result.error500("系统错误");
        }
        return result;
    }
    @ApiOperation(value="统计历年销售额", notes="统计历年销售额")
    @GetMapping(value = "/avgRetailPriceByYear")
    public Result<List<Map<String,Object>>> avgRetailPriceByYear() {
        Result<List<Map<String,Object>>> result = new Result<List<Map<String,Object>>>();
        try {
            List<Map<String,Object>> pageList = retailPurchaseService.avgRetailPriceByYear();
            result.setSuccess(true);
            result.setResult(pageList);
        }catch (Exception e){
            result.error500("系统错误");
        }
        return result;
    }


    /**
     * 统计批发情况
     * @param year
     * @return
     */
    //@AutoLog(value = "统计批发情况")
    @ApiOperation(value="统计批发情况", notes="统计批发情况")
    @GetMapping(value = "/avgWholesalePrice")
    public Result<List<Map<String,Object>>> avgWholesalePrice(@RequestParam(name="year", defaultValue="2019") String year) {
        Result<List<Map<String,Object>>> result = new Result<List<Map<String,Object>>>();
        try {
            List<Map<String,Object>> pageList = wholesalePurchaseService.avgWholesalePrice(year);
            result.setSuccess(true);
            result.setResult(pageList);
        }catch (Exception e){
            result.error500("系统错误");
        }
        return result;
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    //@AutoLog(value = "生态环境-通过id查询")
    @ApiOperation(value="生态环境-通过id查询", notes="生态环境-通过id查询")
    @GetMapping(value = "/queryEcologyById")
    public Result<Ecology> queryEcologyById(@RequestParam(name="id",required=true) String id) {
        Result<Ecology> result = new Result<Ecology>();
        Ecology ecology = ecologyService.getById(id);
        if(ecology==null) {
            result.error500("未找到对应实体");
        }else {
            result.setResult(ecology);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    //@AutoLog(value = "旅游景点-通过id查询")
    @ApiOperation(value="旅游景点-通过id查询", notes="旅游景点-通过id查询")
    @GetMapping(value = "/queryTravelById")
    public Result<Travel> queryTravelById(@RequestParam(name="id",required=true) String id) {
        Result<Travel> result = new Result<Travel>();
        Travel travel = travelService.getById(id);
        if(travel==null) {
            result.error500("未找到对应实体");
        }else {
            result.setResult(travel);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 查询供需成交量大于1000斤的信息，用于系统首页轮播
     * @return
     */
    //@AutoLog(value = "查询供需成交量大于1000斤的信息，用于系统首页轮播")
    @ApiOperation(value="查询供需成交量大于1000斤的信息，用于系统首页轮播", notes="查询供需成交量大于1000斤的信息，用于系统首页轮播")
    @GetMapping(value = "/queryNeedReleaseShow")
    public Result<List<NeedRelease>> queryNeedReleaseShow() {
        Result<List<NeedRelease>> result = new Result<List<NeedRelease>>();
        try {
            List<NeedRelease> list = needReleaseService.queryIndexShow();
            result.setResult(list);
            result.setSuccess(true);
        }catch (Exception e){
            result.error500("系统错误");
        }
        return result;
    }

    /**
     * 查询天气
     * @return
     */
    //@AutoLog(value = "查询天气")
    @ApiOperation(value="查询天气", notes="查询天气")
    @GetMapping(value = "/getWeather")
    public Result<Weather> getWeather() {
        Result<Weather> result = new Result<Weather>();
        try {
            Weather weather = weatherService.getWeather();
            result.setResult(weather);
            result.setSuccess(true);
        }catch (Exception e){
            result.error500("系统错误");
        }
        return result;
    }
}
